﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ToyundaPhone.Controls
{
	public partial class SongListControl : UserControl
	{
		public event EventHandler ListUpdated;

		public SongListControl()
		{
			// Required to initialize variables
			InitializeComponent();
		}

		public SongListViewType ViewType
		{
			get { return (SongListViewType)GetValue(ViewTypeProperty); }
			set { SetValue(ViewTypeProperty, value); }
		}

		// Using a DependencyProperty as the backing store for ViewType.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty ViewTypeProperty =
			DependencyProperty.Register("ViewType", typeof(SongListViewType), typeof(SongListControl), new PropertyMetadata(SongListViewType.Search));

		public string Search
		{
			get { return (string)GetValue(SearchProperty); }
			set { SetValue(SearchProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Search.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty SearchProperty =
			DependencyProperty.Register("Search", typeof(string), typeof(SongListControl), new PropertyMetadata(string.Empty));

		public ToyundaPhone.Database.Playlist Playlist
		{
			get { return (ToyundaPhone.Database.Playlist)GetValue(PlaylistProperty); }
			set { SetValue(PlaylistProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Playlist.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty PlaylistProperty =
			DependencyProperty.Register("Playlist", typeof(ToyundaPhone.Database.Playlist), typeof(SongListControl), new PropertyMetadata(null));

		public string EmptyText
		{
			get { return (string)GetValue(EmptyTextProperty); }
			set { SetValue(EmptyTextProperty, value); }
		}

		// Using a DependencyProperty as the backing store for EmptyText.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty EmptyTextProperty =
			DependencyProperty.Register("EmptyText", typeof(string), typeof(SongListControl), new PropertyMetadata("Vide"));

		public Brush EmptyForeground
		{
			get { return (Brush)GetValue(EmptyForegroundProperty); }
			set { SetValue(EmptyForegroundProperty, value); }
		}

		// Using a DependencyProperty as the backing store for EmptyForeground.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty EmptyForegroundProperty =
			DependencyProperty.Register("EmptyForeground", typeof(Brush), typeof(SongListControl), new PropertyMetadata(null));

		public string RefreshAlertText
		{
			get { return (string)GetValue(RefreshAlertTextProperty); }
			set { SetValue(RefreshAlertTextProperty, value); }
		}

		// Using a DependencyProperty as the backing store for RefreshAlertText.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty RefreshAlertTextProperty =
			DependencyProperty.Register("RefreshAlertText", typeof(string), typeof(SongListControl), new PropertyMetadata(string.Empty));

		private void AddFavorite_Click(object sender, RoutedEventArgs e)
		{
			((sender as FrameworkElement).DataContext as ToyundaPhone.Database.Song).Favorite = true;
			(Application.Current as App).SongDatabase.SubmitChanges();
		}

		private void RemFavorite_Click(object sender, RoutedEventArgs e)
		{
			((sender as FrameworkElement).DataContext as ToyundaPhone.Database.Song).Favorite = false;
			(Application.Current as App).SongDatabase.SubmitChanges();
			if (ViewType == SongListViewType.Favorites)
			{
				OnListUpdated();
			}
		}

		private void OnListUpdated()
		{
			if (ListUpdated != null)
			{
				ListUpdated(this, new EventArgs());
			}
		}

		private void AddList_Click(object sender, RoutedEventArgs e)
		{
			if (Playlist != null)
			{
				Database.SongDatabase db = (Application.Current as App).SongDatabase;
				Database.Song song = (sender as FrameworkElement).DataContext as Database.Song;
				if (db.PlaylistSongs.Count(ps => (ps.SongSong == song && ps.PlaylistPlaylist == Playlist)) == 0)
				{
					Database.PlaylistSong ps = new Database.PlaylistSong
					{
						PlaylistPlaylist = Playlist,
						SongSong = song
					};
					db.PlaylistSongs.InsertOnSubmit(ps);
					Playlist.PlaylistSongs.Add(ps);
					db.SubmitChanges();
				}
			}
			else
			{
				if (SelectPlaylist != null)
				{
					SelectPlaylist(this, new SelectPlaylistEventArgs(() => AddList_Click(sender, e)));
				}
			}
		}

		private void RemList_Click(object sender, RoutedEventArgs e)
		{
			Database.SongDatabase db = (Application.Current as App).SongDatabase;
			Database.Song song = (sender as FrameworkElement).DataContext as Database.Song;
			Database.PlaylistSong ps = Playlist.PlaylistSongs.First(ps1 => ps1.SongSong == song);
			Playlist.PlaylistSongs.Remove(ps);
			db.PlaylistSongs.DeleteOnSubmit(ps);
			db.SubmitChanges();
			if (ViewType != SongListViewType.Favorites)
			{
				OnListUpdated();
			}
		}

		public event EventHandler<SelectPlaylistEventArgs> SelectPlaylist;
	}

	public enum SongListViewType
	{
		Search,
		Favorites,
		Playlist
	}
}
